package com.zyl.blog.processing.exception;

import com.zyl.blog.common.response.Result;
import com.zyl.blog.common.utils.Extreme.LogUtil;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

/**
 * 文件处理全局异常处理器
 * 统一处理文件处理模块中的各种异常
 * 
 * @author FinalFantasy
 * @version v1.0
 * @date 2025/09/01
 */
@RestControllerAdvice
public class GlobalFileProcessingExceptionHandler {

    /**
     * 处理文件处理异常
     */
    @ExceptionHandler(FileProcessingException.class)
    public ResponseEntity<Result<String>> handleFileProcessingException(FileProcessingException e) {
        LogUtil.error("文件处理异常: {}", e, e.getMessage());
        
        Result<String> result = Result.fail("文件处理失败: " + e.getMessage());
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
    }

    /**
     * 处理文件存储异常
     */
    @ExceptionHandler(FileStorageException.class)
    public ResponseEntity<Result<String>> handleFileStorageException(FileStorageException e) {
        LogUtil.error("文件存储异常: {}", e, e.getMessage());
        
        Result<String> result = Result.fail("文件存储失败: " + e.getMessage());
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
    }

    /**
     * 处理文件大小超限异常
     */
    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public ResponseEntity<Result<String>> handleMaxUploadSizeExceeded(MaxUploadSizeExceededException e) {
        LogUtil.warn("文件上传大小超限: {}", e.getMessage());
        
        Result<String> result = Result.fail("文件大小超过限制，请选择较小的文件");
        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(result);
    }

    /**
     * 处理IO异常
     */
    @ExceptionHandler(java.io.IOException.class)
    public ResponseEntity<Result<String>> handleIOException(java.io.IOException e) {
        LogUtil.error("文件IO异常: {}", e, e.getMessage());
        
        Result<String> result = Result.fail("文件操作失败，请重试");
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
    }

    /**
     * 处理安全异常
     */
    @ExceptionHandler(SecurityException.class)
    public ResponseEntity<Result<String>> handleSecurityException(SecurityException e) {
        LogUtil.error("文件安全异常: {}", e, e.getMessage());
        
        Result<String> result = Result.fail("文件操作被拒绝，权限不足");
        return ResponseEntity.status(HttpStatus.FORBIDDEN).body(result);
    }

    /**
     * 处理不支持的操作异常
     */
    @ExceptionHandler(UnsupportedOperationException.class)
    public ResponseEntity<Result<String>> handleUnsupportedOperationException(UnsupportedOperationException e) {
        LogUtil.warn("不支持的操作: {}", e.getMessage());
        
        Result<String> result = Result.fail("不支持的操作: " + e.getMessage());
        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(result);
    }

    /**
     * 处理非法参数异常
     */
    @ExceptionHandler(IllegalArgumentException.class)
    public ResponseEntity<Result<String>> handleIllegalArgumentException(IllegalArgumentException e) {
        LogUtil.warn("参数错误: {}", e.getMessage());
        
        Result<String> result = Result.fail("参数错误: " + e.getMessage());
        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(result);
    }

    /**
     * 处理空指针异常
     */
    @ExceptionHandler(NullPointerException.class)
    public ResponseEntity<Result<String>> handleNullPointerException(NullPointerException e) {
        LogUtil.error("空指针异常", e);
        
        Result<String> result = Result.fail("系统内部错误，请联系管理员");
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
    }

    /**
     * 处理其他所有异常
     */
    @ExceptionHandler(Exception.class)
    public ResponseEntity<Result<String>> handleGenericException(Exception e) {
        LogUtil.error("未处理的异常: {}", e, e.getClass().getSimpleName());
        
        Result<String> result = Result.fail("系统异常，请稍后重试");
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
    }
}